
var ns, toast;
var app = new Vue({
    el: '#app',
    data: {
        productList:[],
        AllTotal:0,//商品总数
        BuzuTotal:0,//商品不足数量
        pageindex: 0,
        pagesize: 10,
        Keywords:'',
        selectId:0,
        StockType:-1,//库存类型 -1 不限制 0 库存充足 1 库存不足
        showSelect:false,
        is_searching:false,
        can_scroll:false,
        current_item_length: 0,

    },
    methods: {
        get_data:function(){
            var that=this;
            that.get_total();
            that.is_searching = true;
            that.can_scroll = false;
            var option={};
            option.pageindex=that.pageindex;
            option.pagesize=that.pagesize;
            option.Keywords=that.Keywords;
            option.StockType=that.StockType;
            option.action='selectproductlist';
            ns.post(option, function(succeed, data, err) {
                console.log(JSON.stringify(that.pageindex));
                that.is_searching = false;
                if(succeed){
                    if (data.productList.length == that.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.pageindex == 0) {
                        that.productList = data.productList;
                    } else {
                        that.productList = that.productList.concat(data.productList);
                    }
                    that.current_item_length = that.productList.length;
                    console.log(JSON.stringify(that.productList));
                    // that.productList=data.productList;
                    that.Total=data.Total;
                }else{
                    that.productList = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    }); 
                }
                
            });
        },
        // 获取数量
        get_total:function(){
            var that=this;
            var option={};
            option.action='totalproductlist';
            ns.post(option, function(succeed, data, err) {
                if(succeed){
                    that.AllTotal=data.AllTotal;
                    that.BuzuTotal=data.WarningTotal;
                    
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    }); 
                }
                
            });

        },
        // 库存筛选
        select_pro:function(type){
            var that = this;
            that.pageindex=0;
            if(type==1){
                that.showSelect=true;
            }else{
                that.showSelect=false;
            }
            that.StockType=type;
            that.get_data();
        },

        // 商品详情
        do_openjilu: function(id) {
            var that = this;
            ns.openWin('kucun_history', '变动记录', {
                id: id,
            });
        },
        // 调整库存
        updatenum:function(item){
            var that=this;
            ns.openWin('product_update_num', '调整库存',{
                item:item,
                headerbg:2
            });
        },
        
        //订单关闭


        set_status_bar: function () {
            var that = this;
            api.setStatusBarStyle({
                style: 'dark',
                color: 'transparent'
            });
        },

    },
});

apiready = function() {
    api.parseTapmode();
    app.set_status_bar();
    ns = window.Foresight.Util;
    ns.init_pull();
    app.get_data();
    // api.addEventListener({
    //     name: 'onlogin'
    // }, function(ret, err) {
    //     app.get_data();
    // });
    api.addEventListener({
        name: 'edit_fee'
    }, function(ret, err){
          app.get_data();
    });
    api.addEventListener({
        name: 'do_open_search'
    }, function(ret, err) {
        ns.openWin('search_frm', '搜索');
    });
    api.addEventListener({
        name: 'do_open_fenlei'
    }, function(ret, err) {
        ns.openWin('product_sort', '商品分类');
    });

}
